package cn.shop.admin.controller;

import cn.shop.admin.entity.backRoleModule.BackRole;
import cn.shop.admin.service.BackRoleService;
import cn.shop.base.MyPage;
import cn.shop.utils.ApiResult;
import cn.shop.utils.annotation.OperationLog;
import cn.shop.utils.constant.Constant;
import cn.shop.utils.enums.OperationType;
import cn.shop.validation.Save;
import cn.shop.validation.Update;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 角色表(BackRole)表控制层
 *
 * @author jzw
 * @since 2021-11-17 11:33:20
 */
@Validated
@RestController
@RequestMapping("/api/v1/backRole")
public class BackRoleController {

	/**
	 * 服务对象
	 */
	@Resource
	private BackRoleService backRoleService;

	/**
	 * 获取全部默认角色id
	 *
	 * @author jy
	 * @since 2022/1/18 10:47
	 */
	@GetMapping("/listDefaultRole")
	public ApiResult<List<Object>> listDefaultRole() {
		return ApiResult.success(backRoleService.listDefaultRole());
	}

	@PutMapping("disOrEnaAccount")
	@OperationLog(module = "角色表模块", type = OperationType.UPDATE, desc = "停启用角色")
	public ApiResult<Map<String, Object>> disOrEnaAccount(@Validated @NotNull(message = "账号id不能为空") Long roleId) {
		BackRole role = new BackRole();
		LambdaUpdateWrapper<BackRole> updateWrapper = Wrappers.lambdaUpdate();
		updateWrapper.eq(BackRole::getInitial, false).setSql("enable = not enable").eq(BackRole::getId, roleId);
		return ApiResult.result(backRoleService.update(role, updateWrapper));
	}

	/**
	 * 获取全部内置角色id
	 *
	 * @author jy
	 * @since 2022/1/18 10:46
	 */
	@GetMapping("/listInitRole")
	public ApiResult<List<Object>> listInitRole() {
		LambdaQueryWrapper<BackRole> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(BackRole::getInitial, true).eq(BackRole::getEnable, true);
		queryWrapper.select(BackRole::getId);
		return ApiResult.success(backRoleService.listObjs(queryWrapper));
	}

	@PutMapping("/setInitRole")
	@OperationLog(module = "角色表模块", type = OperationType.UPDATE, desc = "批量设置内置角色")
	public ApiResult<Map<String, Object>> setInitRole(@RequestBody List<Long> idList) {
		return ApiResult.result(backRoleService.setInitRole(idList));
	}

	/**
	 * @author jy
	 * @since 2020-07-15 14:50:59
	 */
	@PutMapping("/setDefaultRole")
	@OperationLog(module = "角色表模块", type = OperationType.UPDATE, desc = "批量设置默认角色")
	public ApiResult<Map<String, Object>> setDefaultRole(@RequestBody List<Long> idList) {
		return ApiResult.result(backRoleService.setDefaultRole(idList));

	}

	/**
	 * 获取用户关联的角色
	 *
	 * @param id 用户id
	 * @author jy
	 * @since 2022/1/18 10:46
	 */
	@GetMapping("roleByUserId")
	ApiResult<List<Long>> getRoleByUserId(Long id) {
		return ApiResult.success(backRoleService.getRoleByUserId(id));
	}

	/**
	 * 通过角色id查询角色所有权限
	 *
	 * @param id 角色id
	 * @return 所有数据
	 * @author jzw
	 * @since 2021-11-17 11:33:20
	 */
	@GetMapping("queryPermissionIdsByRoleId/{id}")
	public ApiResult<List<Long>> queryPermissionsByRoleId(@Validated @NotNull(message = "角色id不可为空") @PathVariable Long id) {
		return ApiResult.success(backRoleService.queryPermissionsByRoleId(id));
	}

	/**
	 * 查询所有数据
	 *
	 * @param backRole 查询实体
	 * @return 所有数据
	 * @author jzw
	 * @since 2021-11-17 11:33:20
	 */
	@GetMapping("queryAll")
	public ApiResult<List<Map<String, Object>>> queryAll(BackRole backRole) {
		backRole.setEnable(true);
		QueryWrapper<BackRole> query = Wrappers.query(backRole);
		query.select("id value", "name label");
		return ApiResult.success(backRoleService.listMaps(query));
	}

	/**
	 * 分页查询所有数据
	 *
	 * @param page     分页对象
	 * @param backRole 查询实体
	 * @return 所有数据
	 * @author jzw
	 * @since 2021-11-17 11:33:20
	 */
	@GetMapping("queryByPage")
	public ApiResult<MyPage<BackRole>> queryByPage(MyPage<BackRole> page, BackRole backRole,
	                                               @RequestParam(required = false) @DateTimeFormat(pattern = Constant.DATETIME_FORMAT) LocalDateTime startTime,
	                                               @RequestParam(required = false) @DateTimeFormat(pattern = Constant.DATETIME_FORMAT) LocalDateTime endTime) {
		return ApiResult.success(backRoleService.page(page, Wrappers.lambdaQuery(backRole)
				.apply(startTime != null,
						"date_format (create_time,'%Y-%m-%d %H:%i') >= date_format ({0},'%Y-%m-%d %H:%i')", startTime)
				.apply(endTime != null,
						"date_format (create_time,'%Y-%m-%d %H:%i') <= date_format ({0},'%Y-%m-%d %H:%i')", endTime)));
	}

	/**
	 * 通过主键查询单条数据
	 *
	 * @param id 主键
	 * @return 单条数据
	 * @author jzw
	 * @since 2021-11-17 11:33:20
	 */
	@GetMapping("queryOne/{id}")
	public ApiResult<BackRole> selectOne(@PathVariable Serializable id) {
		return ApiResult.success(backRoleService.getById(id));
	}

	/**
	 * 新增数据
	 *
	 * @param backRole 实体对象
	 * @return 新增结果
	 * @author jzw
	 * @since 2021-11-17 11:33:20
	 */
	@PostMapping("insert")
	@OperationLog(desc = "新增角色", type = OperationType.CREATE)
	public ApiResult<Map<String, Object>> insert(@Validated(Save.class) @RequestBody BackRole backRole) {
		return ApiResult.result(backRoleService.insert(backRole));
	}

	/**
	 * 修改数据
	 *
	 * @param backRole 实体对象
	 * @return 修改结果
	 * @author jzw
	 * @since 2021-11-17 11:33:20
	 */
	@PutMapping("update")
	@OperationLog(desc = "修改角色", type = OperationType.UPDATE)
	public ApiResult<Map<String, Object>> update(@Validated(Update.class) @RequestBody BackRole backRole) {
		return ApiResult.result(backRoleService.update(backRole));
	}

	/**
	 * 删除单条数据
	 *
	 * @param id 主键
	 * @return 删除结果
	 * @author jzw
	 * @since 2021-11-17 11:33:20
	 */
	@DeleteMapping("delete/{id}")
	public ApiResult<Map<String, Object>> delete(@PathVariable(required = true) Long id) {
		BackRole role = new BackRole();
		LambdaUpdateWrapper<BackRole> updateWrapper = Wrappers.lambdaUpdate();
		updateWrapper.eq(BackRole::getId, id).eq(BackRole::getInitial, false);
		return ApiResult.result(backRoleService.deleteWithFill(role, updateWrapper));
	}

	/**
	 * 批量删除数据
	 *
	 * @param idList 主键结合
	 * @return 删除结果
	 * @author jzw
	 * @since 2021-11-17 11:33:20
	 */
	@DeleteMapping("batchDelete")
	public ApiResult<Map<String, Object>> batchDelete(@RequestParam("idList") List<Long> idList) {
		BackRole role = new BackRole();
		role.setInitial(false);
		return ApiResult.result(backRoleService.batchDelete(idList, role));
	}

}

